<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="预约编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入预约编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="活动主题" prop="activityName">
        <el-input
          v-model="queryParams.activityName"
          placeholder="请输入活动主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="提交时间" prop="activityDate">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:group:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="groupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="预约编号" align="center" prop="code" />
      <el-table-column label="活动主题" align="center" prop="activityName" />
      <el-table-column label="团体名称" align="center" prop="teamName" />
      <el-table-column label="联系电话" align="center" prop="telphone" />
      <el-table-column label="提交时间" align="center" prop="submitDate" width="180"></el-table-column>
      <el-table-column label="参数人数" align="center" prop="num" />
      <el-table-column :formatter="statusFormatter" label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预约（团体）对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="预约编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入预约编号" />
        </el-form-item>
        <el-form-item label="预约状态" prop="statusName">
          <el-input v-model="form.statusName" placeholder="请输入预约状态" />
        </el-form-item>
        <el-form-item label="参加人数" prop="num">
          <el-input v-model="form.num" placeholder="请输入参加人数" />
        </el-form-item>

        <el-form-item label="活动名称" prop="activityName">
          <el-input v-model="form.activityName" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="活动日期" prop="activityDate">
          <el-input v-model="form.activityDate" placeholder="请输入活动日期" />
        </el-form-item>
        <el-form-item label="活动场次" prop="activityYardTimes">
          <el-input v-model="form.activityYardTimes" placeholder="请输入活动场次" />
        </el-form-item>

         <el-form-item label="团体名称" prop="teamName">
          <el-input v-model="form.teamName" placeholder="请输入团体名称" />
        </el-form-item>
        <el-form-item label="社会统一信用" prop="uniformCode">
          <el-input v-model="form.uniformCode" placeholder="请输入社会统一信用" />
        </el-form-item>
        <el-form-item label="资质证书" prop="idNo">
          <span>资质证书</span>
        </el-form-item>

        <el-form-item label="联系电话" prop="telphone">
          <el-input v-model="form.telphone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="预约时间" prop="orderDate">
          <el-input v-model="form.orderDate" placeholder="请输入预约时间" />
        </el-form-item>
        <el-form-item label="提交时间" prop="submitDate">
          <el-input v-model="form.submitDate" placeholder="请输入提交时间" />
        </el-form-item>
      </el-form>
      <div>团体信息</div>
      <el-table v-loading="loading2" :data="orderList2">
        <el-table-column label="领队姓名" align="center" prop="trueName" />
        <el-table-column :formatter="idTypeFormatter" label="证件类型" align="center" prop="idType" />
        <el-table-column label="证件号码" align="center" prop="idNo" />
        <el-table-column label="领队手机" align="center" prop="mobile" />
        <el-table-column label="团队名单" align="center" prop="mobile" >
           <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate2(scope.row)"
            >查看</el-button>
        </template>
        </el-table-column>
        <el-table-column :formatter="statusFormatter" label="状态" align="center" prop="status" />
        <!-- <el-table-column label="二维码" align="center" prop="qrUrl" /> -->
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 团队名单 -->
    <el-dialog title="团体名单" :visible.sync="open2" width="1100px" append-to-body>
      <el-table v-loading="loading22" :data="memList">
        <el-table-column label="姓名" align="center" prop="trueName" />
        <el-table-column :formatter="idTypeFormatter" label="证件类型" align="center" prop="idType" />
        <el-table-column label="证件号码" align="center" prop="idNo" />
        <el-table-column label="手机号码" align="center" prop="mobile" />
        <el-table-column label="邮箱" align="center" prop="email" />
        <el-table-column :formatter="statusFormatter" label="状态" align="center" prop="status" />
      </el-table>
    
      <pagination
        v-show="total2>0"
        :total="total2"
        :page.sync="queryParams3.pageNum"
        :limit.sync="queryParams3.pageSize"
        @pagination="getList3"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listGroup, getGroup, delGroup, addGroup, updateGroup } from "@/api/system/group";
import { listDetail2 } from "@/api/system/groupDetail";
import { listList } from '@/api/system/groupDetailList'

export default {
  name: "Group",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约（团体）表格数据
      groupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      dateRange: "",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        num: null,
        activityId: null,
        activityName: null,
        activityDate: null,
        activityStartTime: null,
        activityEndTime: null,
        activityYardTimes: null,
        teamName: null,
        uniformCode: null,
        telphone: null,
        orderDate: null,
        submitDate: null,
        status: null,
        completeDate: null,
        cancelDate: null,
        isDel: null,
        version: null,
        createUserId: null,
        createUserName: null,
        updateUserId: null,
        updateUserName: null,
      },

      queryParams2: {
        orderId: null
      },
      loading2: true,
      // 预约（个人）表格数据
      orderList2: [],

      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },

      // 团休

      open2: false,
      loading22: true,
      // 总条数
      total2: 0,
      memList: [],
      queryParams3: {
        activityOrderGroupDetailId: null,
        pageNum: 1,
        pageSize: 10,
      }
      


    };
  },
  created() {
    this.getList();
  },
  methods: {
    statusFormatter(row, column, cellValue) {
      if(cellValue == '1') {
        return '已取消'
      }
      if(cellValue == '2') {
        return '未完成'
      }
      if(cellValue == '3') {
        return '已完成'
      }
      return ''
    },
    idTypeFormatter(row, column, cellValue) {
      if(cellValue == '1') {
        return '学生'
      }
      return ''
    },
    /** 查询预约（团体）列表 */
    getList() {
      this.loading = true;
      if (this.dateRange) {
        this.queryParams.startDate = this.dateRange[0] + " 00:00:00";
        this.queryParams.endDate = this.dateRange[1] + " 23:59:59";
      } else {
        this.queryParams.startDate = "";
        this.queryParams.endDate = "";
      }
      listGroup(this.queryParams).then(response => {
        this.groupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getList2() {
      this.loading2 = true;
      listDetail2(this.queryParams2).then(response => {
        this.orderList2 = response.data;
        this.loading2 = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        num: null,
        activityId: null,
        activityName: null,
        activityDate: null,
        activityStartTime: null,
        activityEndTime: null,
        activityYardTimes: null,
        teamName: null,
        uniformCode: null,
        telphone: null,
        orderDate: null,
        submitDate: null,
        status: "0",
        completeDate: null,
        cancelDate: null,
        isDel: null,
        version: null,
        createUserId: null,
        createUserName: null,
        createTime: null,
        updateUserId: null,
        updateUserName: null,
        updateTime: null
      };
      this.dateRange = "";
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = "";
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约（团体）";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGroup(id).then(response => {
        this.form = response.data;

        if(this.form.status =='1') {
          this.form.statusName = '已取消'
        }else if(this.form.status =='2') {
          this.form.statusName = '未完成'
        }else if(this.form.status =='3') {
          this.form.statusName = '已完成'
        }
        if(this.form.idType =='1') {
           this.form.idTypeName = '学生' 
        }
        this.queryParams2.orderId = id
        this.getList2()
        
        this.open = true;
        this.title = "修改预约（团体）";
      });
    },
    // 团队名单
    async handleUpdate2(row) {
      console.log('handleUpdate2')
      this.queryParams3.activityOrderGroupDetailId = row.id
      await this.getList3()
      this.open2 = true
    },
    async getList3() {
      this.loading22 = true;
      let res = await listList(this.queryParams3)
      this.memList = res.rows;
      this.total2 = res.total;
      this.loading22 = false;
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGroup(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGroup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除预约（团体）编号为"' + ids + '"的数据项？').then(function() {
        return delGroup(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/group/export', {
        ...this.queryParams
      }, `group_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
